package com.ikangtai.shecare.common.baseView.collectionList;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.knowledge.ArticleActivity;
import com.ikangtai.shecare.personal.CollectionActivity;
import com.ikangtai.shecare.common.util.AppLog;

import java.util.List;

public class CollectionAdapter extends BaseAdapter {
    private List<CollectionBean> collectionBeanList;
    private LayoutInflater layoutInflater;
    private CollectionActivity collectionActivity;

    public CollectionAdapter(CollectionActivity collectionActivity, List<CollectionBean> collectionBeanList) {
        this.collectionBeanList = collectionBeanList;
        layoutInflater = LayoutInflater.from(collectionActivity);
        this.collectionActivity = collectionActivity;
    }

    @Override
    public int getCount() {
        return collectionBeanList != null ? collectionBeanList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return collectionBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return collectionBeanList.get(position).getArticleId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.fragment_personal_collection_item_layout, null);
            viewHolder.articleImageView = (ImageView) convertView.findViewById(R.id.articleImageView);
            viewHolder.articleTitleTextView = (TextView) convertView.findViewById(R.id.articleTitleTextView);
            viewHolder.articleDateTextView = (TextView) convertView.findViewById(R.id.articleDateTextView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String articleImageUrl = collectionBeanList.get(position).getLocalCollectImagUrl(collectionActivity);
        Context context = parent.getContext();
        AppLog.i("articleImageUrl = " + articleImageUrl);
        Glide.with(context).load(articleImageUrl)
                .placeholder(R.drawable.article_img_default) // will be displayed before downloaded image
                .error(R.drawable.article_img_default) // will be displayed if the image cannot be loaded
                .into(viewHolder.articleImageView);

        viewHolder.articleTitleTextView.setText(collectionBeanList.get(position).getCollectionTitle());
        viewHolder.articleDateTextView.setText(collectionBeanList.get(position).getCollectionDate());

        viewHolder.articleTitleTextView.setTag(String.valueOf(getItemId(position)));

//        convertView.setOnClickListener(new ArticleClickListener());

        //必须添加此方法，否则listview的onItemLongClick不执行
//        convertView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                return false;
//            }
//        });

        return convertView;
    }

    class ViewHolder {
        ImageView articleImageView;
        TextView articleTitleTextView;
        TextView articleDateTextView;
    }

    class ArticleClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            ViewHolder viewHolder = (ViewHolder) v.getTag();
            String articleId = (String) viewHolder.articleTitleTextView.getTag();
            Intent intent = new Intent(collectionActivity, ArticleActivity.class);
            intent.putExtra("articleId", App.status + "_" + articleId);

            AppLog.i("Adapter articleId = " + articleId);
            collectionActivity.startActivity(intent);
        }
    }
}
